package com.example.service;

/**
 * 功能：
 * 作者：snake
 * 日期：2024/2/4 下午 8:49
 */
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.RecordsTypeEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Certification;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 用户表业务处理
 **/
@Service
public class UserService {

  @Resource
  private UserMapper userMapper;

  @Resource
  private CertificationService certificationService;

  @Value("${server.port:9090}")
  private String port;

  @Value("${ip:localhost}")
  private String ip;

  /**
   * 新增
   */
  public void add(User user) {
    User dbUser = userMapper.selectByUsername(user.getUsername());
    if (ObjectUtil.isNotNull(dbUser)) {
      throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
    }
    if (ObjectUtil.isEmpty(user.getName())) {
      user.setName(user.getUsername());
    }
    String http = "http://" + ip + ":" + port + "/files/";
    user.setAvatar(http + "defult.png");
    user.setRole(RoleEnum.USER.name());
    userMapper.insert(user);
  }

  /**
   * 删除
   */
  public void deleteById(Integer id) {
    userMapper.deleteById(id);
  }

  /**
   * 批量删除
   */
  public void deleteBatch(List<Integer> ids) {
    for (Integer id : ids) {
      userMapper.deleteById(id);
    }
  }

  /**
   * 修改
   */
  public void updateById(User user) {
    User dbUser2 = userMapper.selectByUsername(user.getUsername());
    //  根据当前更新的用户的账号查询数据库  如果数据库存在跟当前更新用户一样账号的数据  那么当前的更新是不合法的  数据重复了
    if (ObjectUtil.isNotEmpty(dbUser2) && !Objects.equals(dbUser2.getId(), user.getId())) {
      throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
    }

    userMapper.updateById(user);
  }

  /**
   * 根据ID查询
   */
  public User selectById(Integer id) {
    return userMapper.selectById(id);
  }

  /**
   * 查询所有
   */
  public List<User> selectAll(User user) {
    return userMapper.selectAll(user);
  }

  /**
   * 分页查询
   */
  public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<User> list = userMapper.selectAll(user);

    return PageInfo.of(list);
  }

  /**
   * 用户登录
   */
  public Account login(Account account) {
    Account dbUser = this.selectByUsername(account.getUsername());
    if (ObjectUtil.isNull(dbUser)) {
      throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
    }
    if (!account.getPassword().equals(dbUser.getPassword())) {   // 比较用户输入密码和数据库密码是否一致
      throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
    }
    // 生成token
    String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
    String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
    dbUser.setToken(token);

    Certification certification = certificationService.selectByUserId(dbUser.getId());  // 查询当前登录用户的骑手认证信息
    dbUser.setIsRider(ObjectUtil.isNotNull(certification) && "通过".equals(certification.getStatus()));
    return dbUser;
  }

  private User selectByUsername(String username) {
    User user = new User();
    user.setUsername(username);
    List<User> userList = this.selectAll(user);
    return CollUtil.isEmpty(userList) ? null : userList.get(0);
  }


  /**
   * 用户注册
   */
  public void register(Account account) {
    User user = new User();
    BeanUtils.copyProperties(account, user);  // 拷贝 账号和密码2个属性
    this.add(user);  // 添加账户信息
  }

  public void charge(Double money) {
    Account currentUser = TokenUtils.getCurrentUser();
    currentUser.setAccount(currentUser.getAccount().add(BigDecimal.valueOf(money)));
    this.updateById((User) currentUser);

    // 记录收支明细
    RecordsService.addRecord("充值", BigDecimal.valueOf(money), RecordsTypeEnum.CHARGE.getValue());
  }
}

